package com.songlanyun.modules.information.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.information.entity.InfoAndDistanceDto;
import com.songlanyun.modules.information.entity.InformationEntity;
import com.songlanyun.modules.information.model.vo.InformationVo;
import com.songlanyun.modules.information.service.InformationService;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;


/**
 * 资讯
 *
 * @author WuChao
 * @email songlanyun@gmail.com
 * @date 2022-11-18 09:52:58
 */
@RestController
@RequestMapping("api/information")
public class InformationApiController extends AbstractApiController {
    @Autowired
    private InformationService informationService;
    @Autowired
    private ShopService shopService;

    /**
     * 查询分页列表
     */
    @GetMapping("/page")
    public R apiPage(@RequestParam Map<String, Object> params) {
        PageUtils page = informationService.apiPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public R apiList(@RequestParam Map<String, Object> params) {
        List<InformationEntity> list = informationService.apiList(params);
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        InformationVo vo = informationService.apiInfo(id);

        return R.ok().put("data", vo);
    }

    /**
     * 信息
     */
    @PostMapping("/infoAndDistance")
    public R infoAndDistance(@RequestBody InfoAndDistanceDto distanceDto) {
        InformationVo vo = informationService.apiInfo(distanceDto.getId());
        if(ObjectUtil.isNotEmpty(distanceDto.getLon())
                && ObjectUtil.isNotEmpty(distanceDto.getLat())
        && CollectionUtil.isNotEmpty(vo.getRecommendShopList())) {
            vo.getRecommendShopList().forEach(o -> {
                shopService.mathCalculate(distanceDto.getLon(), distanceDto.getLat(), o);
            });

            vo.getRecommendShopList().sort(new Comparator<ShopEntity>() {
                @Override
                public int compare(ShopEntity o1, ShopEntity o2) {
                    //升序
                    return o1.getDistance().compareTo(o2.getDistance());
                }
            });
        }
        return R.ok().put("data", vo);
    }

    /**
     * 分享
     */
    @PostMapping("/share/{id}")
    public R share(@PathVariable("id") Long id) {
        informationService.share(getUserId(true), id);
        return R.ok();
    }

}
